package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.auto.*;

import com.model.auto.vo.GetXuanKeVo;
import com.service.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.util.R;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/electiveCourseInfo")
public class ElectiveCourseInfoController{
    @Autowired
    private IElectiveCourseInfoService ElectiveCourseInfoService;

    @Autowired
    private ISchoolLabelService schoolLabelService;

    @Autowired
    private INewSchoolService newSchoolService;

    @Autowired
    private ISysDictService sysDictService;

    //获取高考选科
//    @PostMapping("/getXuanKe")
//    public R getXuanKe(@RequestBody GetXuanKeVo getXuanKeVo) {
//        System.out.println("type是" + getXuanKeVo.getType());
//        System.out.println("page是" + getXuanKeVo.getPageNum());
//        PageInfo<Object> pageInfo = PageHelper.startPage(getXuanKeVo.getPageNum(), getXuanKeVo.getPageSize())
//                .doSelectPageInfo(() -> {
//                    List<ElectiveCourseInfo> list = ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>().
//                            //title不为空时添加查询条件
//                                    eq(getXuanKeVo.getAreaName() != null && !"".equals(getXuanKeVo.getAreaName()), ElectiveCourseInfo::getAreaName, getXuanKeVo.getAreaName()).
//                                    eq(getXuanKeVo.getCourseRequire() != null && !"".equals(getXuanKeVo.getCourseRequire()), ElectiveCourseInfo::getCourseRequire, getXuanKeVo.getCourseRequire()).
//                                    eq(getXuanKeVo.getType() != null && !"".equals(getXuanKeVo.getType()), ElectiveCourseInfo::getType, getXuanKeVo.getType()).
//                                    like(getXuanKeVo.getSchoolName() != null && !"".equals(getXuanKeVo.getSchoolName()), ElectiveCourseInfo::getSchoolName, getXuanKeVo.getSchoolName()).
//                                    like(getXuanKeVo.getMajorName() != null && !"".equals(getXuanKeVo.getMajorName()), ElectiveCourseInfo::getMajorName, getXuanKeVo.getMajorName())
//                    );
//                });
//        return R.ok(pageInfo);
//    }

    //查询所以学校名称
    @GetMapping("/getSchoolName")
    public R getSchoolName() {
        List<ElectiveCourseInfo> list = ElectiveCourseInfoService.getSchoolNameList();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (ElectiveCourseInfo i : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("value", i.getSchoolName());
            result.add(map);
        }
        return R.ok(result);
    }

    //查询所有专业名称
    @GetMapping("/getMajorName")
    public R getMajorName() {
        List<ElectiveCourseInfo> list = ElectiveCourseInfoService.getMajorNameList();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (ElectiveCourseInfo i : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("value", i.getMajorName());
            result.add(map);
        }
        return R.ok(result);
    }

    @PostMapping("/getXingaokao")
    public R getXingaokao(@RequestBody GetXuanKeVo getXuanKeVo){
        List<String> xk = new ArrayList<String>();
        if (!StringUtil.isEmpty(getXuanKeVo.getSchoice())&&!StringUtil.isEmpty(getXuanKeVo.getCchoice())){
            String shou=getXuanKeVo.getSchoice();
            getXuanKeVo.getCchoice().sort(String::compareTo);
            String ci=getXuanKeVo.getCchoice().get(0)+getXuanKeVo.getCchoice().get(1);
            String code=shou+ci;
//            System.out.println("那code是什么"+code);
            List<SysDict> sysDict=sysDictService.list(new LambdaQueryWrapper<SysDict>()
                    .eq(SysDict::getDictName,code)
                    .select(SysDict::getDictName,SysDict::getDictValue)
            );
            if (sysDict.size()>0){
                sysDict.forEach(dict ->{
                    xk.add(String.valueOf(dict.getDictValue()));
                });
            }
        }
        List<String> majorCodeList = new ArrayList<String>();
//        System.out.println("专业名称"+getXuanKeVo.getMajor());
        List<ElectiveCourseInfo> majors = ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>()
                .like(!StringUtil.isEmpty(getXuanKeVo.getMajor()), ElectiveCourseInfo::getMajorName, getXuanKeVo.getMajor())
                .in(!StringUtil.isEmpty(getXuanKeVo.getCourseRequire()),ElectiveCourseInfo::getCourseRequireType,getXuanKeVo.getCourseRequire())
                .in(!StringUtil.isEmpty(xk),ElectiveCourseInfo::getCourseRequireType,xk)
                .select(ElectiveCourseInfo::getSchoolCode)
        );
//        System.out.println("查出来的数量"+majors.size());
        if (majors.size() > 0) {
            majors.forEach(i -> majorCodeList.add(i.getSchoolCode()));
        }else{
            return R.error();
        }
        List<String> Hierarchyscode = new ArrayList<String>();
        List<String> qualificationscode = new ArrayList<String>();
        if (!StringUtil.isEmpty(getXuanKeVo.getHierarchys())) {
            List<NewSchool> Hierarchyss = newSchoolService.getcollegeLevel(getXuanKeVo.getHierarchys());
            if (Hierarchyss.size() > 0) {
                Hierarchyss.forEach(i -> Hierarchyscode.add(i.getNewSchoolid()));
            }
        }
        if (!StringUtil.isEmpty(getXuanKeVo.getQualifications())) {
            List<NewSchool> qualificationss = newSchoolService.getqualifications(getXuanKeVo.getQualifications());
            if (qualificationss.size() > 0) {
                qualificationss.forEach(i -> qualificationscode.add(i.getNewSchoolid()));
            }
        }
        String city=getXuanKeVo.getCity().toString();
        String schoolType=getXuanKeVo.getType().toString();
        List<NewSchool> pageInfo;
        List<Object> citys= Arrays.asList("陕西省","安徽省","北京市","重庆市","福建省","广东省","广西","甘肃省","贵州省","河北省",
                "河南省","海南省","湖北省","湖南省","黑龙江省","吉林省","江苏省","江西省","辽宁省","内蒙古","宁夏","青海省","上海市",
                "四川省","山东省","山西省","天津市","西藏","新疆","云南省","浙江省");
        List<Object> schoolTypes=Arrays.asList("综合","工科","财经","农业","林业","医药","师范","体育","政法","民族","语言"
                ,"军事","艺术");
        if (!StringUtil.isEmpty(getXuanKeVo.getCity())&&city.equals("[其他]")){
            if (!StringUtil.isEmpty(getXuanKeVo.getType())&&schoolType.equals("[其他]")){
                             pageInfo = newSchoolService.list(new LambdaQueryWrapper<NewSchool>()
                        .in(!StringUtil.isEmpty(majorCodeList), NewSchool::getNewSchoolid, majorCodeList)
                        .in(!StringUtil.isEmpty(Hierarchyscode), NewSchool::getNewSchoolid, Hierarchyscode)
                        .in(!StringUtil.isEmpty(qualificationscode), NewSchool::getNewSchoolid, qualificationscode)
                        .notIn(NewSchool::getProvince, citys)
                        .in(!StringUtil.isEmpty(getXuanKeVo.getNature()), NewSchool::getNature, getXuanKeVo.getNature())
                        .notIn(NewSchool::getSchoolType, schoolTypes)
                        .like(!StringUtil.isEmpty(getXuanKeVo.getSchool()), NewSchool::getSchoolName, getXuanKeVo.getSchool())
                        .eq(!StringUtil.isEmpty(getXuanKeVo.getArrangement()),NewSchool::getArrangement,getXuanKeVo.getArrangement())
                        .orderByAsc(NewSchool::getNewSchoolid)
                );
            }else{
                pageInfo = newSchoolService.list(new LambdaQueryWrapper<NewSchool>()
                        .in(!StringUtil.isEmpty(majorCodeList), NewSchool::getNewSchoolid, majorCodeList)
                        .in(!StringUtil.isEmpty(Hierarchyscode), NewSchool::getNewSchoolid, Hierarchyscode)
                        .in(!StringUtil.isEmpty(qualificationscode), NewSchool::getNewSchoolid, qualificationscode)
                        .notIn(NewSchool::getProvince, citys)
                        .in(!StringUtil.isEmpty(getXuanKeVo.getNature()), NewSchool::getNature, getXuanKeVo.getNature())
                        .in(!StringUtil.isEmpty(getXuanKeVo.getType()), NewSchool::getSchoolType, getXuanKeVo.getType())
                        .like(!StringUtil.isEmpty(getXuanKeVo.getSchool()), NewSchool::getSchoolName, getXuanKeVo.getSchool())
                        .eq(!StringUtil.isEmpty(getXuanKeVo.getArrangement()),NewSchool::getArrangement,getXuanKeVo.getArrangement())
                        .orderByAsc(NewSchool::getNewSchoolid)
                );
            }
        }else {
            if (!StringUtil.isEmpty(getXuanKeVo.getType())&&schoolType.equals("[其他]")){
                pageInfo = newSchoolService.list(new LambdaQueryWrapper<NewSchool>()
                        .in(!StringUtil.isEmpty(majorCodeList), NewSchool::getNewSchoolid, majorCodeList)
                        .in(!StringUtil.isEmpty(Hierarchyscode), NewSchool::getNewSchoolid, Hierarchyscode)
                        .in(!StringUtil.isEmpty(qualificationscode), NewSchool::getNewSchoolid, qualificationscode)
                        .in(!StringUtil.isEmpty(getXuanKeVo.getCity()),NewSchool::getProvince, getXuanKeVo.getCity())
                        .in(!StringUtil.isEmpty(getXuanKeVo.getNature()), NewSchool::getNature, getXuanKeVo.getNature())
                        .notIn(NewSchool::getSchoolType, schoolTypes)
                        .like(!StringUtil.isEmpty(getXuanKeVo.getSchool()), NewSchool::getSchoolName, getXuanKeVo.getSchool())
                        .eq(!StringUtil.isEmpty(getXuanKeVo.getArrangement()),NewSchool::getArrangement,getXuanKeVo.getArrangement())
                        .orderByAsc(NewSchool::getNewSchoolid)
                );
            }else{
                pageInfo = newSchoolService.list(new LambdaQueryWrapper<NewSchool>()
                        .in(!StringUtil.isEmpty(majorCodeList), NewSchool::getNewSchoolid, majorCodeList)
                        .in(!StringUtil.isEmpty(Hierarchyscode), NewSchool::getNewSchoolid, Hierarchyscode)
                        .in(!StringUtil.isEmpty(qualificationscode), NewSchool::getNewSchoolid, qualificationscode)
                        .in(!StringUtil.isEmpty(getXuanKeVo.getCity()),NewSchool::getProvince, getXuanKeVo.getCity())
                        .in(!StringUtil.isEmpty(getXuanKeVo.getNature()), NewSchool::getNature, getXuanKeVo.getNature())
                        .in(!StringUtil.isEmpty(getXuanKeVo.getType()), NewSchool::getSchoolType, getXuanKeVo.getType())
                        .like(!StringUtil.isEmpty(getXuanKeVo.getSchool()), NewSchool::getSchoolName, getXuanKeVo.getSchool())
                        .eq(!StringUtil.isEmpty(getXuanKeVo.getArrangement()),NewSchool::getArrangement,getXuanKeVo.getArrangement())
                        .orderByAsc(NewSchool::getNewSchoolid)
                );
            }
        }



//        List<NewSchool> schools = pageInfo.getList();
        //统计分页后的学校编号
        ArrayList<Object> endCodeList = new ArrayList<>();
        pageInfo.forEach(i -> endCodeList.add(i.getNewSchoolid()));
        //根据学校编号查出专业
//        List<ElectiveCourseInfo> list = endCodeList.size() == 0 ? new ArrayList<>() :
//                ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>()
//                        .in(ElectiveCourseInfo::getSchoolCode, endCodeList)
//                );
//
//        List<ElectiveCourseInfo> majorlimit = ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>()
//                .like(!StringUtil.isEmpty(getXuanKeVo.getMajor()), ElectiveCourseInfo::getMajorName, getXuanKeVo.getMajor())
//                .in(!StringUtil.isEmpty(getXuanKeVo.getCourseRequire()),ElectiveCourseInfo::getCourseRequireType,getXuanKeVo.getCourseRequire())
//                .in(!StringUtil.isEmpty(xk),ElectiveCourseInfo::getCourseRequireType,xk)
//        );
//        list.forEach(l->{
//            majorlimit.forEach(m->{
//                if(l.getId().equals(m.getId())){
//                    l.setColor("red");
//                }
//            });
//        });

        //查出物理参考分数
        List<SysDict> li=sysDictService.list(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getDictType,"new_school_li")
        );

        //查出历史参考分数
        List<SysDict> wen=sysDictService.list(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getDictType,"new_school_wen")
        );

//        System.out.println("排序后的学校" + pageInfo);
        for (NewSchool j : pageInfo) {
//            ArrayList<ElectiveCourseInfo> majorList = new ArrayList<>();
//            for (ElectiveCourseInfo i : list) {
//                if (i.getSchoolCode().equals(j.getNewSchoolid())) {
//                    majorList.add(i);
//                }
//            }
//            j.setChildren(majorList);
            for (SysDict l:li){
                if (l.getDictName().equals(j.getNewSchoolid())){
                    j.setCankao_li(String.valueOf(l.getDictValue()));
                }
            }

            for (SysDict w:wen){
                if (w.getDictName().equals(j.getNewSchoolid())){
                    j.setCankao_wen(String.valueOf(w.getDictValue()));
                }
            }
        }
//        pageInfo.setList(schools);
        return R.ok(pageInfo);
    }

    @PostMapping("/getSchoolMajor")
    public R getSchoolMajor(@RequestBody GetXuanKeVo getXuanKeVo){
        List<String> xkCode = new ArrayList<String>();
        if (!StringUtil.isEmpty(getXuanKeVo.getSchoice())&&!StringUtil.isEmpty(getXuanKeVo.getCchoice())){
            String shou=getXuanKeVo.getSchoice();
            getXuanKeVo.getCchoice().sort(String::compareTo);
            String ci=getXuanKeVo.getCchoice().get(0)+getXuanKeVo.getCchoice().get(1);
            String code=shou+ci;
            System.out.println("那code是什么"+code);
            List<SysDict> sysDict=sysDictService.list(new LambdaQueryWrapper<SysDict>()
                    .eq(SysDict::getDictName,code)
            );
            if (sysDict.size()>0){
                sysDict.forEach(dict ->{
                    xkCode.add(String.valueOf(dict.getDictValue()));
                });
            }
        }
        List<ElectiveCourseInfo> list=ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>()
                .eq(!StringUtil.isEmpty(getXuanKeVo.getSchoolCode()),ElectiveCourseInfo::getSchoolCode,getXuanKeVo.getSchoolCode())
        );
        List<ElectiveCourseInfo> limit=ElectiveCourseInfoService.list(new LambdaQueryWrapper<ElectiveCourseInfo>()
                .eq(!StringUtil.isEmpty(getXuanKeVo.getSchoolCode()),ElectiveCourseInfo::getSchoolCode,getXuanKeVo.getSchoolCode())
                .like(!StringUtil.isEmpty(getXuanKeVo.getMajor()), ElectiveCourseInfo::getMajorName, getXuanKeVo.getMajor())
                .in(!StringUtil.isEmpty(getXuanKeVo.getCourseRequire()),ElectiveCourseInfo::getCourseRequireType,getXuanKeVo.getCourseRequire())
                .in(!StringUtil.isEmpty(xkCode),ElectiveCourseInfo::getCourseRequireType,xkCode)
        );
        if (list.size()>0){
            list.forEach(l->{
                limit.forEach(m->{
                    if(l.getId().equals(m.getId())){
                        l.setColor("red");
                    }
                });
            });
        }
                return R.ok(list);
    }
}
